﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Medical.EntityType;
using Sector7.Framework.Enums;
using Sector7.Framework.DB;
using Sector7.Medical.DAO.Standard;
using Sector7.Framework.Utility;

namespace Sector7.Medical.ADDM.Windows
{
    /// <summary>
    /// AddInterviewItemWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddInterviewTemplateItemWindow : Window
    {
        public AddInterviewTemplateItemWindow()
        {
            InitializeComponent();
        }

        private void LoadInterviewTemplateItem(T7_InterviewTemplateItem item)
        {
            txbItemName.Text = item.TemplateItemName;
            txbPercentInTotal.Text = item.PercentInTotal.ToString();
        }

        public T7_InterviewTemplate InterviewTemplate
        {
            get { return m_temp; }
            set { m_temp = value; }
        }

        public T7_InterviewTemplateItem InterviewTemplateItem
        {
            set
            {
                m_item = value;
                LoadInterviewTemplateItem(m_item);
            }
            get { return m_item; }
        }

        public CrudType Crud
        {
            get { return m_crud; }
            set { m_crud = value; }
        }

        private T7_InterviewTemplate m_temp = null;
        private T7_InterviewTemplateItem m_item = null;
        private CrudType m_crud = CrudType.Unknown;

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            var newItem = new T7_InterviewTemplateItem();
            newItem.TemplateItemName = txbItemName.Text;
            newItem.PercentInTotal = Convert.ToDecimal(txbPercentInTotal.Text);

            object result = null;

            switch (m_crud)
            {
                case CrudType.Create:
                    newItem.TemplateId = m_temp.TemplateId;

                    result = DAOFactory.GetDAO<InterviewTemplateItemDAO>().Create(newItem);
                    newItem.TemplateItemId = Convert.ToInt32(result);
                    break;
                case CrudType.Update:
                    newItem.TemplateId = m_temp.TemplateId;
                    newItem.TemplateItemId = m_item.TemplateItemId;

                    var delta = ClientUtil.GetDelta(m_item, newItem);
                    if (delta.DeltaProperties.Count > 0)
                    {
                        delta.Conditions.Add("TemplateItemId", m_item.TemplateItemId);
                        result = DAOFactory.GetDAO<InterviewTemplateDAO>().Update(delta);
                    }
                    break;
                default:
                    return;
            }

            m_item = newItem;

            this.DialogResult = result != null;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
